<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <?php include "../public/substt.php";?>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>项目信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=engin_speed','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=engin_speed','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>进度填报单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">关联月计划</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="monthid" class="layui-input monthid" onclick="choose_speed_month()">
                            <div class="choose_div" id="monthid" key="1"></div>
                            <span class="choose_one" onclick="choose_speed_month()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>填报月份</label>
                        <div class="layui-input-block">
                            <input type="text" name="month" id="month" placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ym_cus">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>填报名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>填报明细</span></div>
                        <div>
                            <button type="button" onclick="choose_speed_month_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从月计划导入</button>
                            <button type="button" onclick="choose_speed_zong_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从总计划导入</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>编码</th>
                            <th>分部名称</th>
                            <th>分项名称</th>
                            <th>单位</th>
                            <th>本月计划完成量</th>
                            <th>本月计划工期</th>
                            <th>实际开工日期</th>
                            <th><text>*</text>本月施工天数</th>
                            <th><text>*</text>本月完成量</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>

            <?php include '../public/flowqita.php'?>
            <?php include '../public/upinfo.php'?>
        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    chu_flow_edit_yushen();


    $("#commentForm").validate({
        rules: {
            name: {required: true},
            month: {required: true},
        },
        messages:{
            name: {required: "请输入"},
            month: {required: "请选择"},
        }
    });


    function choose_speed_month(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }
        openPage('../public/choose_speed_month.php?dom=monthid&page=engin_speed&enginid='+enginid,'80%','80%')
    }




    function choose_speed_month_list(){
        var monthid = get_choose_val('monthid');
        if(!monthid){
            facade.error("请先选择关联月计划");return false;
        }
        openPage('../public/choose_speed_month_list.php?page=engin_speed&monthid='+monthid,'80%','80%');
    }

    function choose_speed_zong_list(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }
        openPage('../public/choose_speed_zong_list.php?page=engin_speed&enginid='+enginid,'80%','80%');
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        $('.bdate_ym_cus').datetimepicker({
            language: 'zh-CN',//显示中文
            format: 'yyyy-mm',//显示格式
            startView: '3',
            minView: "3",//设置只显示到月份
            initialDate: new Date(),//初始化当前日期
            autoclose: true,//选中自动关闭
            todayBtn: true//显示今日按钮
        }).on('changeDate',function (e) {
            var month = $("#month").val();
            $("#name").val(month+'进度填报')
            $("#monthid").html('');
            $("#list_list").html('');
        });

        facade.ajax({type: 'get', url: ask_flowdata_edit_engin_speed, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                chu_flow_qian(data);


                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                }

                var monthinfo = res.data.monthinfo;
                if(monthinfo){
                    set_choose_btn('monthid',monthinfo.id,monthinfo.name);
                }


                $("#number").val(data.number);
                $("#name").val(data.name);
                $("#month").val(data.month);
                $("#explain").val(data.explain);


                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" readonly class="layui-input code disabled"></td>';
                listAddStr += '<td><input type="text" readonly class="layui-input buname disabled"></td>';
                listAddStr += '<td><input type="text" readonly class="layui-input xiangname disabled"></td>';
                listAddStr += '<td><input type="text" readonly class="layui-input unit disabled"></td>';
                listAddStr += '<td><input type="number" readonly class="layui-input finnum disabled"></td>';
                listAddStr += '<td><input type="number" readonly class="layui-input duration disabled"></td>';
                listAddStr += '<td><input type="text" class="layui-input findt bdate_ymd"></td>';
                listAddStr += '<td><input type="number" class="layui-input shiday"></td>';
                listAddStr += '<td><input type="number" class="layui-input shinum"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);

                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" id="'+list[key].id+'" zongid="'+list[key].zongid+'" monthid="'+list[key].monthid+'" class="layui-input code" value="'+list[key].code+'"></td>';
                        str += '<td><input type="text" readonly class="layui-input buname disabled" value="'+list[key].buname+'"></td>';
                        str += '<td><input type="text" readonly class="layui-input xiangname disabled" value="'+list[key].xiangname+'"></td>';
                        str += '<td><input type="text" readonly class="layui-input unit disabled" value="'+list[key].unit+'"></td>';
                        str += '<td><input type="number" readonly class="layui-input finnum disabled" value="'+list[key].finnum+'"></td>';
                        str += '<td><input type="number" readonly class="layui-input duration disabled" value="'+list[key].duration+'"></td>';
                        str += '<td><input type="text" class="layui-input findt bdate_ymd" value="'+list[key].findt+'"></td>';
                        str += '<td><input type="number" class="layui-input shiday" value="'+list[key].shiday+'"></td>';
                        str += '<td><input type="number" class="layui-input shinum" value="'+list[key].shinum+'"></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }

                public_upload_file_load('upload_files', res.data.files)
                render_date();
                render_from();
                initParseMoney();
            }
        });


        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.isshen = $(this).attr('isshen') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            data.field.enginid = get_choose_val('enginid');
            data.field.monthid = get_choose_val('monthid');
            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();
                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }
                if(!validRes){return false;}
            }





            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var id = $(this).find('.code').attr('id') || 0;
                    var zongid = $(this).find('.code').attr('zongid') || 0;
                    var monthid = $(this).find('.code').attr('monthid') || 0;
                    var code = $(this).find('.code').val();
                    var buname = $(this).find('.buname').val();
                    var xiangname = $(this).find('.xiangname').val();
                    var unit = $(this).find('.unit').val();
                    var finnum = $(this).find('.finnum').val();
                    var duration = $(this).find('.duration').val();
                    var findt = $(this).find('.findt').val();
                    var shiday = $(this).find('.shiday').val();
                    var shinum = $(this).find('.shinum').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        if(!shiday){
                            throw new Error('第'+index+'行，请输入本月施工天数');
                        }
                        if(!shinum){
                            throw new Error('第'+index+'行，请输入本月完成量');
                        }

                        var chongKey = check_chongfu(i, zongid);
                        if(chongKey){
                            throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                        }
                    }



                    list.push({
                        zongid:zongid,
                        monthid:monthid,
                        code:code,
                        buname:buname,
                        xiangname:xiangname,
                        unit:unit,
                        finnum:finnum,
                        duration:duration,
                        findt:findt,
                        shiday:shiday,
                        shinum:shinum,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);

            public_flow_submit(data.field, ask_flowdata_add_engin_speed, ask_flowdata_edit_engin_speed, $(this).attr('but'));
            return false;
        });

    });

    //检测重复
    function check_chongfu(index, zongid){
        console.log(zongid)
        try {
            $('#list_list tr').each(function (i) {
                var cur_zongid = $(this).find('.code').attr('zongid') || 0;
                if(index != i){
                    if(zongid == cur_zongid){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }



    function choose_enginid_delete() {
        $("#monthid").html('');
        $("#list_list").html('');
    }

    function choose_monthid_delete() {
        $("#list_list").html('');
    }

</script>
</body>
</html>
